/**
 * Copyright (C) 2010-2013 Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.rocketmq.example.quickstart;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;


/**
 * Producer，发送消息
 * 
 */
public class ProducerDelayTime {
    public static void main(String[] args) throws MQClientException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        //消息达到5M时开始压缩
        producer.setCompressMsgBodyOverHowmuch(1024*5);
        //文件最大20M
        producer.setMaxMessageSize(1024*20);
        producer.start();

        /**
          * 在服务器端（rocketmq-broker端）的属性配置文件中加入以下行：
		  * messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
		  * 描述了各级别与延时时间的对应映射关系。
		  * 这个配置项配置了从1级开始，各级延时的时间，可以修改这个指定级别的延时时间；
		  * 时间单位支持：s、m、h、d，分别表示秒、分、时、天；
		  * 默认值就是上面声明的，可手工调整；
		  * 默认值已够用，不建议修改这个值。
         */
        for (int i = 0; i < 1000; i++) {
            try {
                Message msg = new Message("TopicTest",// topic
                    "TagA",// tag
                    ("Hello RocketMQ " + i).getBytes()// body
                        );
                //设置消息延时 10s 消费
                msg.setDelayTimeLevel(3);
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);
            }
            catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
